package com.system.jfm;
import com.system.jfm.Components.*;
import com.system.Daos.AdminDao;
import com.system.Models.Admin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class LoginFrame extends JFrame {
    public LoginFrame(){
        initFrame();
        this.setVisible(true);
    }
    public void initFrame(){
        JFrame c = this;
        Image iconImage = Toolkit.getDefaultToolkit().getImage("src/com/system/image/icon.png");
        this.setIconImage(iconImage);

        this.setSize(550,400);
        this.setTitle("StuHub | 登录");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().setBackground(Color.white);

        ImageIcon imageIcon=new ImageIcon("src/com/system/image/StuHub.png");
        JButton title_btn=new JButton(imageIcon);
        title_btn.setBounds(130,60,imageIcon.getIconWidth(),imageIcon.getIconHeight());
        title_btn.setBorderPainted(false);

        JLabel title_label = new JLabel("登入 StuHub");
        title_label.setBounds(this.getWidth()/2-30,60,200,30);
        Font font = new Font( "黑体",Font.PLAIN, 20);
        title_label.setFont(font);

        LabelTextField username_input = new LabelTextField("用户名:   ",250,30,0.2);
        username_input.setLocation((int)(this.getWidth()/2-username_input.getWidth()/2),title_label.getY()+50);
        username_input.setPlaceHolder("请输入用户名");
        username_input.setBackground(Color.white);
        username_input.label.setHorizontalAlignment(SwingConstants.RIGHT);

        LabelPasswordTextField pwd_input = new LabelPasswordTextField("密码:   ",250,30,0.2);
        pwd_input.setLocation(this.getWidth()/2-pwd_input.getWidth()/2,username_input.getY()+50);
        pwd_input.setBackground(Color.white);
        pwd_input.label.setHorizontalAlignment(SwingConstants.RIGHT);

        JButton register_btn = new JButton("注册");
        JButton login_btn = new JButton("登录");

        register_btn.setBounds(320,pwd_input.getY()+50,60,40);
        login_btn.setBounds(200,pwd_input.getY()+50,60,40);

        register_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                c.setVisible(false);
                new RegisterFrame();
            }
        });

        login_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if(username_input.getText().equals("请输入用户名")){
                    JOptionPane.showMessageDialog(null,"用户名不能为空","提示",JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                if(pwd_input.getText().equals("请输入密码")){
                    JOptionPane.showMessageDialog(null,"用户名不能为空","提示",JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                String name = username_input.getText();
                String pwd = pwd_input.getText();

                Admin admin = new Admin(name,pwd);
                try {
                    if(new AdminDao(admin).vailPassword()){
                        c.setVisible(false);
                        new MainTable(admin);
                    }else{
                        new Message("密码或者用户名错误");
                    }
                } catch (SQLException ex) {
                    new Message("密码或者用户名错误");
                    throw new RuntimeException(ex);
                }

            }
        });

        this.setLayout(null);
        this.setResizable(false);
        this.setLocationRelativeTo(null);

        this.add(title_label);
        this.add(username_input);
        this.add(pwd_input);
        this.add(login_btn);
        this.add(register_btn);
        this.add(title_btn);

    }
}
